﻿using System;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;

namespace MvcCms.Service.Code
{
    public class Files
    {
        public static string Unzipfile(string zipPath, string unzipPath)
        {
            try
            {
                var s = new ZipInputStream(File.OpenRead(zipPath));
                ZipEntry zEntry;
                FileStream streamWriter = null;
                //   string tempDir = "";
                var sFileName = "";

                while ((zEntry = s.GetNextEntry()) != null)
                {
                    var directoryName = "";
                    //    string strdirectoryName = "";
                    try
                    {

                        directoryName = Path.GetDirectoryName(zEntry.Name);
                        if (!Directory.Exists(unzipPath + "\\" + directoryName))
                        {
                            Directory.CreateDirectory(unzipPath + "\\" + directoryName);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new ApplicationException(ex.Message);
                    }

                    sFileName = Path.GetFileName(zEntry.Name);
                    try
                    {
                        if (sFileName != String.Empty)
                        {
                            streamWriter = File.Create(unzipPath + "\\" + sFileName);

                            var size = 20480;
                            var data = new byte[20480];
                            while (true)
                            {
                                size = s.Read(data, 0, data.Length);
                                if (size > 0)
                                {
                                    streamWriter.Write(data, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                            }

                            streamWriter.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new ApplicationException(ex.Message);
                    }
                }

                if (streamWriter != null)
                {
                    streamWriter.Close();
                }
                s.Close();
                return sFileName;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public static void ResizeImage(string originalFile, string newFile, int newWidth, int maxHeight, bool onlyResizeIfWider)
        {
            System.Drawing.Image fullsizeImage = System.Drawing.Image.FromFile(originalFile);

            // Prevent using images internal thumbnail
            fullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
            fullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

            if (onlyResizeIfWider)
            {
                if (fullsizeImage.Width <= newWidth)
                {
                    newWidth = fullsizeImage.Width;
                }
            }

            int newHeight = fullsizeImage.Height * newWidth / fullsizeImage.Width;
            if (newHeight > maxHeight)
            {
                // Resize with height instead
                newWidth = fullsizeImage.Width * maxHeight / fullsizeImage.Height;
                newHeight = maxHeight;
            }

            System.Drawing.Image newImage = fullsizeImage.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero);

            // Clear handle to original file so that we can overwrite it if necessary
            fullsizeImage.Dispose();

            // Save resized picture
            newImage.Save(newFile);
        }
    }
}
